package main

import "fmt"

func findMaxRect(x, y int, matrix [][]byte) int {
	i := x
	ret := 1
	minWidth := len(matrix[0])
	if x == 1 && y == 2 {
		fmt.Println()
	}
	//minHeight := len(matrix)
	for ; i < len(matrix); i++ {
		j := y

		if matrix[i][j] == 0 {
			break
		}

		for ; j < len(matrix[0]); j++ {
			if matrix[i][j] != '1' {
				break
			}
		}

		width := j - y
		height := i - x + 1

		if minWidth > width {
			minWidth = width
		}

		if height > minWidth {
			break
		}

		if height*height > ret {
			ret = height * height
		}
	}

	return ret
}

func maximalSquare(matrix [][]byte) int {
	max := 0
	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[0]); j++ {
			if matrix[i][j] == '1' {
				if ret := findMaxRect(i, j, matrix); ret > max {
					max = ret
				}
			}
		}
	}

	return max
}

func main() {
	fmt.Println(maximalSquare([][]byte{{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}}))

}
